﻿/********************************************************************************
     File:																
            Manager.aspx.cs                         
     Description:
            商品信息管理
     Author:									
            DDBuildTools
            http://DDBuildTools.supesoft.com
     Finish DateTime:
            2012/8/23 20:00:26
     History:
*********************************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using BingZi.HY.Core;
using BingZi.WebControls;
using NewLife.Web;
using System.Collections.Generic;
using NewLife.Reflection;

 

namespace BingZi.UIProduct
{
    public partial class Manager : System.Web.UI.Page
    {
        public Int32 IDX = (Int32)Common.sink("IDX", MethodType.Get, 10, 0, DataType.Int);
        public string CMD = (string)Common.sink("CMD", MethodType.Get, 10, 1, DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            BingZiPermission.CheckPagePermission(CMD);
            if (!Page.IsPostBack)
            {
                OnStart();
            }
        }
        
        /// <summary>
        /// 开始操作
        /// </summary>
        private void OnStart()
        {
            app_Product ut = app_Product.FindByKeyForEdit(IDX);
            OnStartData(ut);
            switch (CMD)
            { 
                case "New":
                    TabOptionItem1.Tab_Name = HeadMenuWebControls1.HeadOPTxt = "增加商品信息";
                    Hidden_Disp();
                    break;
                case "List":
                    TabOptionItem1.Tab_Name = HeadMenuWebControls1.HeadOPTxt = "查看商品信息";
                    Hidden_Input();
                    ButtonOption.Visible = false;
                    AddEditButton();
                    break;
                case "Edit":
                    TabOptionItem1.Tab_Name = HeadMenuWebControls1.HeadOPTxt = "修改商品信息";
                    Hidden_Disp();
                    AddDeleteButton();
                    break;
                case "Delete":
                    if (ut.Delete() > 0)
                    {
                        EventMessage.MessageBox(1, "删除成功", string.Format("删除ID:{0}成功!", IDX), Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx"));
                    }
                    else {
                        EventMessage.MessageBox(1, "删除失败", string.Format("删除ID:{0}失败!", IDX), Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
                    }
                    break;
                default :
                    EventMessage.MessageBox(2, "不存在操作字符串!", "不存在操作字符串!", Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
                    break;
            }
        }

        /// <summary>
        /// 增加修改按钮
        /// </summary>
        private void AddEditButton()
        {
            HeadMenuButtonItem bi = new HeadMenuButtonItem();
            bi.ButtonPopedom = PopedomType.Edit;
            bi.ButtonName = "商品信息";
            bi.ButtonUrl = string.Format("?CMD=Edit&IDX={0}", IDX);
            HeadMenuWebControls1.ButtonList.Add(bi);
        }


        /// <summary>
        /// 增加删除按钮
        /// </summary>
        private void AddDeleteButton()
        {
            HeadMenuButtonItem bi = new HeadMenuButtonItem();
            bi.ButtonPopedom = PopedomType.Delete;
            bi.ButtonName = "商品信息";
            bi.ButtonUrlType = UrlType.JavaScript;
            bi.ButtonUrl = string.Format("DelData('?CMD=Delete&IDX={0}')", IDX);
            HeadMenuWebControls1.ButtonList.Add(bi);

            HeadMenuButtonItem bi1 = new HeadMenuButtonItem();
            bi1.ButtonPopedom = PopedomType.List;
            bi1.ButtonIcon = "back.gif";
            bi1.ButtonName = "返回";
            bi1.ButtonUrl = string.Format("?CMD=List&IDX={0}", IDX);
            HeadMenuWebControls1.ButtonList.Add(bi1);
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="ut"></param>
        private void OnStartData(app_Product ut)
        {
        CategoryID_Input.Text = CategoryID_Disp.Text = ut.CategoryID.ToString();
         
        app_Category.BindDropDownTree(CategoryID_Input, app_Category.GetListTree());
        WebHelper.SelectedListByValue(CategoryID_Input, ut.CategoryID);


                Title_Input.Text = Title_Disp.Text = ut.Title.ToString();
                Subtitle_Input.Text = Subtitle_Disp.Text = ut.Subtitle.ToString();
                ContentText_Input.Text = ContentText_Disp.Text = ut.ContentText.ToString();
                ContactUs_Input.Text = ContactUs_Disp.Text = ut.ContactUs.ToString();
                PriceNow_Input.Text = PriceNow_Disp.Text = ut.PriceNow.ToString();
                PriceOriginal_Input.Text = PriceOriginal_Disp.Text = ut.PriceOriginal.ToString();
                MinNumber_Input.Text = MinNumber_Disp.Text = ut.MinNumber.ToString();
                InventoryNumber_Input.Text = InventoryNumber_Disp.Text = ut.InventoryNumber.ToString();
                SalesNumber_Input.Text = SalesNumber_Disp.Text = ut.SalesNumber.ToString();
                StartTime_Input.Text = StartTime_Disp.Text = ut.StartTime.ToString("yyyy-MM-dd");
                EndTime_Input.Text = EndTime_Disp.Text = ut.EndTime.ToString("yyyy-MM-dd");
                IsTop_Input.Checked = ut.IsTop == (Int32)EnumProductIsTop.True;
                IsTop_Disp.Text = EnumHelper.GetEnumTextVal(ut.IsTop, typeof(EnumProductIsTop));

                WebHelper.BindList(Status_Input, typeof(EnumProductStatus));
                WebHelper.SelectedListByValue(Status_Input, ut.Status);
                Status_Disp.Text = EnumHelper.GetEnumTextVal(ut.Status, typeof(EnumProductStatus));


                //图片信息
                if ((ut.Picture + "").Trim() != "")
                {
                    Picture_Disp.ImageUrl = Common.BuildDownFileUrl(string.Format(ut.Picture, "s_"));
                    Picture_Disp.ToolTip = Common.BuildDownFileUrl(string.Format(ut.Picture, ""));
                }
                else
                    Picture_Disp.Visible = false;


               LastTime_Disp.Text = ut.LastTime.ToString();
               LastUser_Disp.Text = sys_UserTable.FindUserNameByID( ut.LastUser);
                 LastIP_Disp.Text =  ut.LastIP.ToString();
                
        }

        /// <summary>
        /// 隐藏输入框
        /// </summary>
        private void Hidden_Input()
        {
        CategoryID_Input.Visible = false;
         
        Title_Input.Visible = false;
        Subtitle_Input.Visible = false;
        ContentText_Input.Visible = false;
        ContactUs_Input.Visible = false;
        PriceNow_Input.Visible = false;
        PriceOriginal_Input.Visible = false;
        MinNumber_Input.Visible = false;
        InventoryNumber_Input.Visible = false;
        SalesNumber_Input.Visible = false;
        StartTime_Input.Visible = false;
        EndTime_Input.Visible = false;
        IsTop_Input.Visible = false;
        Status_Input.Visible = false;
        UpdateTable.Visible = true;
        Picture_Input.Visible = false;
        }

        /// <summary>
        /// 隐藏显示框
        /// </summary>
        private void Hidden_Disp()
        {
        CategoryID_Disp.Visible = false;
        
        Title_Disp.Visible = false;
        Subtitle_Disp.Visible = false;
        ContentText_Disp.Visible = false;
        ContactUs_Disp.Visible = false;
        PriceNow_Disp.Visible = false;
        PriceOriginal_Disp.Visible = false;
        MinNumber_Disp.Visible = false;
        InventoryNumber_Disp.Visible = false;
        SalesNumber_Disp.Visible = false;
        StartTime_Disp.Visible = false;
        EndTime_Disp.Visible = false;
        IsTop_Disp.Visible = false;
        Status_Disp.Visible = false;
        LastTime_Disp.Visible = false;
        LastUser_Disp.Visible = false;
        LastIP_Disp.Visible = false;
        UpdateTable.Visible = false;
        }

        /// <summary>
        /// 增加/修改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
        
            int CategoryID_Value = (int)Common.sink(CategoryID_Input.UniqueID, MethodType.Post, 10, 1, DataType.Int);
           
            string Title_Value = (string)Common.sink(Title_Input.UniqueID, MethodType.Post, 512, 1, DataType.Str);
            string Subtitle_Value = (string)Common.sink(Subtitle_Input.UniqueID, MethodType.Post, 256, 1, DataType.Str);
            string ContentText_Value = (string)Common.sink(ContentText_Input.UniqueID, MethodType.Post, 2147483647, 0, DataType.Str);
            string ContactUs_Value = (string)Common.sink(ContactUs_Input.UniqueID, MethodType.Post, 2147483647, 0, DataType.Str);
            double PriceNow_Value = (double)Common.sink(PriceNow_Input.UniqueID, MethodType.Post, 53, 1, DataType.Double);
            double PriceOriginal_Value = (double)Common.sink(PriceOriginal_Input.UniqueID, MethodType.Post, 53, 1, DataType.Double);
            int MinNumber_Value = (int)Common.sink(MinNumber_Input.UniqueID, MethodType.Post, 10, 1, DataType.Int);
            int InventoryNumber_Value = (int)Common.sink(InventoryNumber_Input.UniqueID, MethodType.Post, 10, 1, DataType.Int);
            int SalesNumber_Value = (int)Common.sink(SalesNumber_Input.UniqueID, MethodType.Post, 10, 1, DataType.Int);
            DateTime StartTime_Value = (DateTime)Common.sink(StartTime_Input.UniqueID, MethodType.Post, 20, 1, DataType.Dat);
            DateTime EndTime_Value = (DateTime)Common.sink(EndTime_Input.UniqueID, MethodType.Post, 20, 1, DataType.Dat);
            
            String IsTop_Value =  (string)Common.sink(IsTop_Input.UniqueID, MethodType.Post, 5, 0, DataType.Str);
                
            int Status_Value = (int)Common.sink(Status_Input.UniqueID, MethodType.Post, 3, 1, DataType.Int);
 
            app_Product ut = app_Product.FindByKeyForEdit(IDX);
            
            ut.CategoryID = CategoryID_Value;
            
            ut.Title = Title_Value;
            ut.Subtitle = Subtitle_Value;
            ut.ContentText = ContentText_Value;
            ut.ContactUs = ContactUs_Value;
            ut.PriceNow = PriceNow_Value;
            ut.PriceOriginal = PriceOriginal_Value;
            ut.MinNumber = MinNumber_Value;
            ut.InventoryNumber = InventoryNumber_Value;
            ut.SalesNumber = SalesNumber_Value;
            ut.StartTime = StartTime_Value;
            ut.EndTime = EndTime_Value;
            ut.IsTop = IsTop_Value == "on" ? (Int32)EnumProductIsTop.True : (Int32)EnumProductIsTop.False;
            ut.Status = Status_Value;
            ut.LastTime = DateTime.Now;
            ut.LastUser = Common.Get_UserID;
            ut.LastIP = WebHelper.UserHost;

         
                string b_InfoPhoto_Value = UpPhoto(Picture_Input);
                if (b_InfoPhoto_Value != "")
                {
                    FileUpLoadCommon.DeleteFile(string.Format(ut.Picture, "s_"));
                    FileUpLoadCommon.DeleteFile(string.Format(ut.Picture, ""));
                    ut.Picture = b_InfoPhoto_Value;
                }
          

            Int32 rInt = 0;
            if (CMD == "New")
            {
                rInt = ut.Insert();
            }
            else if (CMD == "Edit")
            {
                rInt = ut.Update();
            }
            else
            {
                EventMessage.MessageBox(2, "不存在操作字符串!", "不存在操作字符串!", Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
            }
        
            if ( rInt> 0)
            {
                string OpTxt = string.Format("增加商品信息成功!(ID:{0})", rInt);
                if (CMD == "Edit")
                    OpTxt = string.Format("修改商品信息成功!(ID:{0})",IDX);
                EventMessage.MessageBox(1, "操作成功", OpTxt, Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx"));
            }
            else if (rInt == -2)
            {
                EventMessage.MessageBox(1, "操作失败", "操作失败,存在相同的键值(用户名/数据)!", Icon_Type.Alert, Common.GetHomeBaseUrl("Default.aspx"));
            }
            else
            {
                EventMessage.MessageBox(1, "操作失败", string.Format("操作失败,返回值:{0}!", rInt), Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
            }
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="fu"></param>
        /// <returns></returns>
        private string UpPhoto(FileUpload fu)
        {
            if (fu.PostedFile.ContentLength > 0)
            {
                string FileDir = string.Format("{0}{1}/{2}/", Common.UpLoadDir, "Product", DateTime.Now.ToString("yyyyMMdd"));
                FileUpLoadCommon fc = new FileUpLoadCommon(FileDir, false);
                fc.SaveFile(fu, true);
                return FileDir + "{0}" + fc.newFileName;
            }
            return "";
        }
    }
}
